package com.pcbly.web.business.config;

import java.util.*;

import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;

import com.pcbly.web.bean.system.ConfigBean;
import com.pcbly.web.business.BaseServiceImp;
import com.pcbly.web.business.BusinessException;
import com.pcbly.web.persistence.config.ConfigDao;

public class ConfigServiceImp extends BaseServiceImp<ConfigBean>
    implements ConfigService {

  private static final Log logger = LogFactory.getLog(ConfigServiceImp.class);

  private ConfigDao configDao;

  public ConfigServiceImp() {
	  super();
  }

  /**
   *
   * @param id String
   * @return Config
   * @todo Implement this com.laoer.bbscs.service.ConfigService method
   */
  public ConfigBean findConfigByID(String id) throws Exception {
    return configDao.findConfigByID(id);
  }

  /**
   *
   * @return List
   * @todo Implement this com.laoer.bbscs.service.ConfigService method
   */
  public List<ConfigBean> findConfigs() throws Exception {
    return configDao.getAll();
  }

  /**
   *
   * @param config Config
   * @return Config
   * @throws BbscsException
   * @todo Implement this com.laoer.bbscs.service.ConfigService method
   */
  public ConfigBean updateConfig(ConfigBean config) throws BusinessException {
    try {
      return configDao.updateConfig(config);
    }
    catch (Exception ex) {
      logger.error(ex);
      throw new BusinessException(ex);
    }
  }

  public void updateAllConfigs(HashMap<String,ConfigBean> configs) throws BusinessException {
    Iterator<ConfigBean> it = configs.values().iterator();
    try {
      while (it.hasNext()) {
    	  ConfigBean config = (ConfigBean) it.next();
    	  configDao.updateConfig(config);
      }
    }
    catch (Exception ex) {
      logger.error(ex);
      throw new BusinessException(ex);
    }
  }


  public void setConfigDao(ConfigDao configDao) {
    this.configDao = configDao;
  }
}
